﻿hpxAdminApp.controller('beansDetailController', function ($rootScope, $scope, $stateParams, $state, API_URL, NgTableParams, commissionService) {
	$scope.filter = {
		deadlineTime1: '',
		deadlineTime2: '',
		budget: '',
		//充值弹框
		rechargeAmount: 0,
		rechargeDraftBean: 0,
		conversionRule: 1,
		defaultOption: 1,
		ratios: [
            {
            	lable: '1:1',
            	value: 1
            },
            {
            	lable: '1:1.1',
            	value: 1.1
            },
            {
            	lable: '1:1.2',
            	value: 1.2
            },
            {
            	lable: '1:1.3',
            	value: 1.3
            },
            {
            	lable: '1:1.4',
            	value: 1.4
            },
            {
            	lable: '1:1.5',
            	value: 1.5
            }
		],
		//修改弹框
		operationalAction: '2',
		draftBean: '',
		remark: '',
		//导出url
		api: API_URL + '/draftbean/account/export'
	}
	if ($stateParams) {
		$scope.model = {
			enterpriseId: $stateParams.id,
			enterpriseName: '',
			balance: ''
		}
	}
	//查询列表
	$scope.tableParams = new NgTableParams({ sorting: { 'createTime': 'desc' } }, {
		getData: function (params) {
			return commissionService.getBeansDetail(params, $stateParams.id, $scope.filter).then(function (data) {
				$scope.first = $scope.getFirst(params);
				$scope.dataList = data.items;
				if (data) {
					return data.items
				}
			});
		}
	});

	//查询企业汇票豆账户
	$scope.getBeansAccount = function () {
		commissionService.getBeansAccount($scope.model.enterpriseId).then(function (data) {
			if (data) {
				$scope.model.enterpriseName = data.enterpriseName;
				$scope.model.balance = data.availableBalance;
			}
		});
	}
	$scope.getBeansAccount()

	//查询
	$scope.filterList = function () {
		$scope.tableParams.reload()
	}
	//弹框
	$scope.edit = function (way) {
		$('#modal-edit'+way).modal('show');
	};


	$("#start_time").datetimepicker({
		format: "yyyy-mm-dd",
		autoclose: true,
		minView: "month",
		maxView: "decade",
		todayBtn: true,
		pickerPosition: "bottom-left",
		language: 'zh-CN'
	}).on("click", function (ev) {
		$("#start_time").datetimepicker("setEndDate", $("#end_time").val());
	}).on('changeDate', function (e) {
		$scope.filter.deadlineTime1 = $("#start_time").val();
	});

	$("#end_time").datetimepicker({
		format: "yyyy-mm-dd",
		autoclose: true,
		minView: "month",
		maxView: "decade",
		todayBtn: true,
		pickerPosition: "bottom-left",
		language: 'zh-CN'
	}).on("click", function (ev) {
		$("#end_time").datetimepicker("setStartDate", $("#start_time").val());
	}).on('changeDate', function (e) {
		$scope.filter.deadlineTime2 = $("#end_time").val();
	});

	//充值金额监听
	$scope.changeAmount = function () {
		if ($scope.filter.rechargeAmount == '' || $scope.filter.rechargeAmount == 0 || $scope.filter.rechargeAmount == undefined) {
			$scope.filter.rechargeDraftBean = 0
		} else {
		    $scope.filter.rechargeDraftBean = parseInt($scope.filter.defaultOption * $scope.filter.rechargeAmount * 1)
		}
	}

	//更改比例
	$scope.changeRatio = function (data) {
	    if ($scope.filter.defaultOption) {
	        switch ($scope.filter.defaultOption) {
				case 1:
					$scope.filter.conversionRule = 1
					break;
				case 1.1:
					$scope.filter.conversionRule = 2
					break;
				case 1.2:
					$scope.filter.conversionRule = 3
					break;
				case 1.3:
					$scope.filter.conversionRule = 4
					break;
				case 1.4:
					$scope.filter.conversionRule = 5
					break;
				case 1.5:
					$scope.filter.conversionRule = 6
					break;
			}
			$scope.filter.rechargeDraftBean = parseInt($scope.filter.defaultOption * $scope.filter.rechargeAmount * 1)
		}
	}
	//充值保存
	$scope.save = function () {
		commissionService.update($scope.model.enterpriseId, $scope.filter).then(function () {
			$scope.tableParams.reload();
			$scope.editForm.$setPristine();
			$('#modal-edit').modal('hide');
			$scope.getBeansAccount();
		});
	};
	//修改保存
	$scope.modify = function () {
		commissionService.modify($scope.model.enterpriseId, $scope.filter).then(function () {
			$scope.tableParams.reload();
			$scope.editForm.$setPristine();
			$('#modal-edit1').modal('hide');
			$scope.getBeansAccount();
		});
	}
	//导出Excel

	//function sub() {
	//	$.ajax({
	//		cache: true,
	//		type: "POST",
	//		url: API_URL + '/draftbean/account/export',
	//		data:$('#formId').serialize(),// 你的formid
	//		async: false,
	//		error: function(request) {
	//			alert("Connection error:"+request.error);
	//		},
	//		success: function(data) {
	//			alert("SUCCESS!");
	//		}
	//	});
	//}

	$scope.exportTable = function () {
		//commissionService.exportTable($scope.model.enterpriseId, $scope.filter).then(function (data) {
		//	window.open(data.data)
		//})
		if ($scope.dataList.length == 0) {
			return
		} else {
			window.open(API_URL + '/draftbean/account/export?enterpriseId=' + $scope.model.enterpriseId + '&budget=' + $scope.filter.budget + '&startTime=' + $scope.filter.deadlineTime1 + '&endTime=' + $scope.filter.deadlineTime2)
		}

		//var exportData = {
		//	'enterpriseId': $scope.model.enterpriseId,                //类型：Number  必有字段  备注：无
		//	'budget': $scope.filter.budget,                //类型：Number  必有字段  备注：无
		//	'startTime': $scope.filter.deadlineTime1,                //类型：String  必有字段  备注：无
		//	'endTime': $scope.filter.deadlineTime2,                //类型：String  必有字段  备注：无
		//	'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJyb2xlX2lkIjoxLCJpc3MiOiJmamFrd2puazMya2pqYWRrOTIxamRpamFqZHUxIiwiY3VzdG9tZXJfaWQiOjAsImVudGVycHJpc2VfaWQiOjAsImlhdCI6MTU0MjA3MjMwNSwibG9jYXRpb25faWQiOjB9.fZeUvCt9EL-BkBZiWhTW0eB5Ejd7HvxD0aRDd3aNwrg'
		//}
		//let form = $("<form>"); //创建form标签

		//form.attr("style","display:none");
		//form.attr("method","post");//设置请求方式
		//form.attr("action", API_URL + '/draftbean/account/export'); //action属性设置请求路径
		//$("body").append(form); //页面添加form标签

		//let input1 = $("<input>") //创建input标签
		//input1.attr("type","hidden") //设置隐藏域
		//input1.attr("name","data") //设置发送后台数据的参数名
		//input1.attr("value",JSON.stringify(exportData));
		//form.append(input1);
		//form.submit();//表单提交即可下载！
	}
});
